<?php


class Product extends Base_Controller
{
	
	public function __construct()
	{
		parent::__construct();
		$this->load->model('product_mdl','product');
		$this->load->model('stock_mdl','stock');
		$this->load->library('form_validation');
	}
	
	
	
	/**
	 * 奖项列表
	 * 
	 */
	public function index()
	{
		/*分页设置*/
		$ptype = intval($this->uri->rsegment(4));
		$page = $this->uri->rsegment(6);
	
		$page = ($page && is_numeric($page)) ? intval($page) : 1;
		$limit = 10;
		$offset = ($page - 1) * $limit;
		$pagination = '';
	
		$count = $this->product->get_product_count($ptype);
		$this->load->library('pagination');
		$config['base_url'] = base_url().'/adminroot/product/index/ptype/'.$ptype.'/page/';
		$config['total_rows'] = $count;
		//设置url上第几段用于传递分页器的偏移量
		$config ['uri_segment'] = 7;	 
		$config['per_page'] = $limit; 
		$config['use_page_numbers'] = TRUE;
		$this->pagination->initialize($config); 
				
		$data = array();
		$data['list'] = $this->product->get_product_list(true,$limit, $offset,'DESC',$ptype);
		
		//取得所有STOCK
		$stock = array();
		$tmp = array();
		$stock = $this->stock->get_stock_list( $page = false, $limit = 0, $offset = 0, $order = 'DESC', $ptype = 0 );
		if ( !empty( $stock )) {
			foreach( $stock as $k => $v ) {
				$tmp[$v['productid']][] = $v; 
			}
		}
		
		$data['stock'] = $tmp;
		
		$data['page'] = $this->pagination->create_links();	
		$this->load->view( 'adminroot/list_product',$data );
	}
	
	/**
	 * 添加奖品
	 * 
	 */
	
	public function addproduct()
	{
		
		$this->_load_validation_rules();
		if($this->form_validation->run() == FALSE)
		{
			
			$this->load->view('adminroot/add_product');
		}
		else 
		{
			$updata = array();
			if(!empty($_FILES['img'])){
				
				$config['upload_path'] = './upload/productimg/';
				$config['allowed_types'] = 'gif|jpg|png';
				$config['encrypt_name'] = true;
				$config['overwrite'] = false;
			  	$this->load->library('upload', $config);
 
				  if ( ! $this->upload->do_upload('img'))
				  {
				   $error = array('error' => $this->upload->display_errors());
				  	var_dump($error);
				  }
				  $updata = $this->upload->data();
			}
			$data['productname'] = $this->input->post('productname');
			$data['ptype'] = $this->input->post('ptype');
			$data['img'] = $updata['file_name'];
			$data['content'] = $this->input->post('content');
			$data['pprice'] = $this->input->post('pprice');
			$result = $this->product->add_product($data);
			redirect('adminroot/product');
		}
		
	}
	
	
	
		/**
	 * 编辑，更新奖项
	 */
	public function edit_product()
	{
		$id = intval($this->uri->rsegment(4));
		$data['product'] = $this->product->get_product_by_id($id);
		$this->_load_validation_rules();
		if($this->form_validation->run() == FALSE)
		{
			$this->load->view('adminroot/edit_product',$data);
		}
		else 
		{
			$updata = array();
			if(!empty($_FILES['img']['name'])){
				
				$config['upload_path'] = './upload/productimg/';
				$config['allowed_types'] = 'gif|jpg|png';
				$config['encrypt_name'] = true;
				$config['overwrite'] = false;
			  	$this->load->library('upload', $config);
 
				  if ( ! $this->upload->do_upload('img'))
				  {
				   $error = array('error' => $this->upload->display_errors());
				  	var_dump($error);
				  }
				  $updata = $this->upload->data();
				  $img =$updata['file_name'];
			}
			else 
			{
				$img = $this->input->post('img2');
			}
			 $this->product->update_product(
				intval($this->input->post('id')),
				array(
					'productname' => $this->input->post('productname'),
					'ptype' =>  $this->input->post('ptype'),
					'img' => $img,
					'content' => $this->input->post('content'),
					'pprice' => $this->input->post('pprice'),
				)
			);
			redirect('adminroot/product');
		}
		
	}
	
	
	/**
	 * 删除奖品
	 * 
	 */
	public function del_product()
	{
		$id = intval($this->uri->rsegment(4));
		$result = $this->product->del_product($id);
		redirect('adminroot/product/index');
	}
	
	
	//peoduct表单验证
	
	private function _load_validation_rules()
	{
		$this->form_validation->set_error_delimiters('<li>', '</li>');
		$this->form_validation->set_rules('productname','奖品名称','required|xss_clean');
		$this->form_validation->set_rules('ptype','奖品类型','required|xss_clean');

	}
}